<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\User;
use App\Models\Dict;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(), function (Grid $grid) {
            $grid->column('nickname');
            $grid->column('phone');
            $grid->column('sex')->display(function ($v) {
                return Dict::where('namespace', 'user_sex')->where('key', $v)->value('value');
            });
            $grid->column('borthday');
            $grid->column('invite_code');
            $grid->column('balance');
            $grid->column('superior_id')->display(function ($v) {
                if ($v){
                    return \App\Models\User::where('uuid', $v)->value('nickname');
                }else{
                    return '暂无';
                }

            });
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
//            $grid->disableEditButton();
//            $grid->disableCreateButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->field('nickname');
            $show->field('phone');
            $show->field('sex');
            $show->field('borthday');
            $show->field('invite_code');
            $show->field('balance');
            $show->field('superior_id');
            $show->field('created_at');
            $show->field('updated_at');
            $show->disableEditButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->text('nickname');
            $form->image('headimgurl')->autoUpload()->required()->name(function ($image){
                return date('Y-m-d') . '/' . md5(uniqid()).'.' . $image->guessExtension();
            });
            $form->text('phone');
            $form->password('password');
            $form->text('openid');
            $form->text('sex');
            $form->date('borthday');
            $form->hidden('invite_code');
            $form->number('balance');
            $form->text('superior_id');
            $form->saving(function (Form $form) {
                $form->invite_code = hashid_encode(env('KEYWORDS'));
                $form->password = Hash::make($form->password);
            });
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
